import {
  convertEmoji,
  getCONF
} from '../util'
import Wechat from "../wechat"
const CONF = getCONF()
export interface Contact_Data {
  RemarkName: string;
  DisplayName: string;
  Uin: number;
  UserName: string;
  NickName: string;
  HeadImgUrl: string;
  ContactFlag: number;
  MemberCount: number;
  MemberList: Contact_Data[];
  HideInputBarFlag: number;
  Sex: number;
  Signature: string;
  VerifyFlag: number;
  OwnerUin: number;
  PYInitial: string;
  PYQuanPin: string;
  RemarkPYInitial: string;
  RemarkPYQuanPin: string;
  StarFriend: number;
  AppAccountFlag: number;
  Statues: number;
  AttrStatus: number;
  Province: string;
  City: string;
  Alias: string;
  SnsFlag: number;
  UniFriend: number;
  ChatRoomId: number;
  KeyWord: string;
  EncryChatRoomId: string;
}
/* Contact Object Example
{
  "Uin": 0,
  "UserName": "",
  "NickName": "",
  "HeadImgUrl": "",
  "ContactFlag": 3,
  "MemberCount": 0,
  "MemberList": [],
  "RemarkName": "",
  "HideInputBarFlag": 0,
  "Sex": 0,
  "Signature": "",
  "VerifyFlag": 8,
  "OwnerUin": 0,
  "PYInitial": "",
  "PYQuanPin": "",
  "RemarkPYInitial": "",
  "RemarkPYQuanPin": "",
  "StarFriend": 0,
  "AppAccountFlag": 0,
  "Statues": 0,
  "AttrStatus": 0,
  "Province": "",
  "City": "",
  "Alias": "Urinxs",
  "SnsFlag": 0,
  "UniFriend": 0,
  "DisplayName": "",
  "ChatRoomId": 0,
  "KeyWord": "gh_",
  "EncryChatRoomId": ""
}
*/
export function getUserByUserName(memberList: Contact_Data[], UserName: string): Contact_Data | null {
  if (!memberList.length) return null;
  return memberList.find((contact: Contact_Data) => contact.UserName === UserName)
}

export function getDisplayNameEx(contact: Contact_Data): string {
  if (isRoomContact(contact)) {
    if (contact.MemberCount >= 2) {
      return '[群] ' + (contact.RemarkName || contact.DisplayName || contact.NickName ||
        `${getDisplayName(contact.MemberList[0])}、${getDisplayName(contact.MemberList[1])}`)
    } else {
      return '[群] ' + (contact.RemarkName || contact.DisplayName || contact.NickName ||
        `${getDisplayName(contact.MemberList[0])}`)
    }
  } else {
    return contact.DisplayName || contact.RemarkName || contact.NickName || contact.UserName
  }
}
export function getDisplayName(contact: Contact_Data): string {

  return contact.DisplayName || contact.RemarkName || contact.NickName

}
export function isRoomContact(contact: Contact_Data): boolean {
  return contact.UserName ? /^@@|@chatroom$/.test(contact.UserName) : false
}

export function isSpContact(contact: Contact_Data): boolean {
  return CONF.SPECIALUSERS.indexOf(contact.UserName) >= 0
}

export function isPublicContact(contact: Contact_Data): boolean {
  return (contact.VerifyFlag & CONF.MM_USERATTRVERIFYFALG_BIZ_BRAND) != 0
}
export class Contact implements Contact_Data {
  public OrignalNickName: string = "";
  public OrignalRemarkName: string = "";
  public OrignalDisplayName: string = "";
  public isSelf: boolean = false;

  //
  public RemarkName: string = "";
  public DisplayName: string = "";
  public Uin: number = 0;
  public UserName: string = "";
  public NickName: string = "";
  public HeadImgUrl: string = "";
  public ContactFlag: number = 0;
  public MemberCount: number = 0;
  public MemberList: Contact_Data[] = [];
  public HideInputBarFlag: number = 0;
  public Sex: number = 0;
  public Signature: string = "";
  public VerifyFlag: number = 0;
  public OwnerUin: number = 0;
  public PYInitial: string = "";
  public PYQuanPin: string = "";
  public RemarkPYInitial: string = "";
  public RemarkPYQuanPin: string = "";
  public StarFriend: number = 0;
  public AppAccountFlag: number = 0;
  public Statues: number = 0;
  public AttrStatus: number = 0;
  public Province: string = "";
  public City: string = "";
  public Alias: string = "";
  public SnsFlag: number = 0;
  public UniFriend: number = 0;
  public ChatRoomId: number = 0;
  public KeyWord: string = "";
  public EncryChatRoomId: string = "";
  //
  // public static createInstance(data: Contact_Data, instance: any): Contact {
  //   let ins = Object.setPrototypeOf(data, Contact);
  //   ins.init(instance);
  //   return ins;
  // }

  constructor(data: Contact_Data, instance: Wechat) {
    for (let key in data) {
      this[key] = data[key];
    }
    this.init(instance);
  }
  public init(instance: Wechat) {
    this.OrignalNickName = this.NickName
    this.OrignalRemarkName = this.RemarkName
    this.OrignalDisplayName = this.DisplayName
    this.NickName = convertEmoji(this.NickName)
    this.RemarkName = convertEmoji(this.RemarkName)
    this.DisplayName = convertEmoji(this.DisplayName)
    this.isSelf = this.UserName === instance.user.UserName;

  }
  public getDisplayName(): string {
    return getDisplayName(this)
  }
  public canSearch(keyword: string): boolean {
    if (!keyword) return false
    keyword = keyword.toUpperCase()

    let isSatisfy = (key: string) => (key || '').toUpperCase().indexOf(keyword) >= 0
    return (
      isSatisfy(this.RemarkName) ||
      isSatisfy(this.RemarkPYQuanPin) ||
      isSatisfy(this.NickName) ||
      isSatisfy(this.PYQuanPin) ||
      isSatisfy(this.Alias) ||
      isSatisfy(this.KeyWord)
    )
  }
}

